
/*!
 * \file     tetriswidget.cpp
 * \brief    TetrisWidget implementation.
 * \author   Duriez Franck
 * \author   Carton Paul-Henri
 * \version  0.1
 */

#include <math.h>
#include "assert_macros.h"
#include "constant.h"
#include "tetriswidget.h"
#include "coloredsquare.h"
#include "coloredshape.h"

TetrisWidget::TetrisWidget(QWidget* parent) : /*{{{*/
		QSFMLWidget(parent, QPoint(0,0), QSize(100,100), 0),
		m_on_pause(false),
		m_game_over(false),
		m_grid(this, this, &m_elapsed_time),
		m_next_shape_manager(this, &m_elapsed_time),
		m_score_manager(this, &m_elapsed_time)
{
} /*}}}*/

TetrisWidget::~TetrisWidget() /*{{{*/
{
} /*}}}*/

void TetrisWidget::gameOver() /*{{{*/
{
	m_game_over = true;
	m_on_pause  = true;
} /*}}}*/

void TetrisWidget::update() /*{{{*/
{
	if (!m_on_pause) /*{{{*/
	{
		// grid
		m_grid.update();
		//DEBUGGER(m_grid.toString());

		// next shape
		m_next_shape_manager.update();

		// score
		m_score_manager.update();
	} /*}}}*/
	else /*{{{*/
	{
	} /*}}}*/

	draw();
} /*}}}*/

void TetrisWidget::draw() /*{{{*/
{
	// global background
	Draw(m_background_sprite);

	// grid
	m_grid.draw();

	// next shape
	m_next_shape_manager.draw();

	// score
	m_score_manager.draw();

	if (m_game_over) /*{{{*/
	{
		sf::String gameover("Game Over", sf::Font::GetDefaultFont(), 60.f);
		sf::FloatRect rect = gameover.GetRect();
		gameover.SetPosition(WINDOW_WIDTH/2 - rect.GetWidth()/2, WINDOW_HEIGHT/2 - rect.GetHeight()/2);
		Draw(gameover);
		return;
	} /*}}}*/

	if (m_on_pause) /*{{{*/
	{
		sf::String onpause("Pause", sf::Font::GetDefaultFont(), 60.f);
		sf::FloatRect rect = onpause.GetRect();
		onpause.SetPosition(WINDOW_WIDTH/2 - rect.GetWidth()/2, WINDOW_HEIGHT/2 - rect.GetHeight()/2);
		Draw(onpause);
		return;
	} /*}}}*/
} /*}}}*/

void TetrisWidget::initialize() /*{{{*/
{
	// tetris background
	m_background_image.LoadFromFile(GLOBAL_BACKGROUND_IMAGE);
	m_background_sprite.SetImage(m_background_image);
	float x_b = ((float) WINDOW_WIDTH) / m_background_image.GetWidth();
	float y_b = ((float) WINDOW_HEIGHT) / m_background_image.GetHeight();
	m_background_sprite.SetScale(x_b, y_b);

	// move down timer
	m_move_down_timer.setInterval(1000);
	QObject::connect(&m_move_down_timer, SIGNAL(timeout()),
			&m_grid, SLOT(moveTheShapeDown()));
	m_move_down_timer.start();
	QObject::connect(&m_score_manager, SIGNAL(levelUp()),
			this, SLOT(setMoveDownSpeed()));

	// grid
	m_grid.setPosition(Vector2f(GRID_POS_X,GRID_POS_Y));
	QObject::connect(&m_grid, SIGNAL(shapeAbsorbed()),
			this, SLOT(putTheNextShapeInTheGrid()));
	QObject::connect(&m_grid, SIGNAL(gameOver()),
			this, SLOT(gameOver()));

	// current shape
	Shape* ashape = m_next_shape_manager.transfertShape();
	m_grid.setShape(ashape);

	// score displayer
	QObject::connect(&m_grid, SIGNAL(linesDestroyed(int)),
			&m_score_manager, SLOT(raiseScore(int)));

	// tetriswidget
	//Clear();
} /*}}}*/

void TetrisWidget::setMoveDownSpeed() /*{{{*/
{
	m_move_down_timer.setInterval(1000 * (int) sqrt(m_score_manager.getLevel()));
} /*}}}*/

void TetrisWidget::putTheNextShapeInTheGrid() /*{{{*/
{
	m_grid.setShape(m_next_shape_manager.transfertShape());
} /*}}}*/

void TetrisWidget::keyPressEvent(QKeyEvent* event) /*{{{*/
{
	switch (event->key()) {
	case Qt::Key_Escape :
			qApp->quit();
			break;
	case Qt::Key_Shift :
			if (m_grid.getShape() && !m_on_pause) m_grid.getShape()->rotateAction.restart(true);
			break;
	case Qt::Key_Control :
			if (m_grid.getShape() && !m_on_pause) m_grid.getShape()->antiRotateAction.restart(true);
			break;
	case Qt::Key_Left :
			if (m_grid.getShape() && !m_on_pause) m_grid.getShape()->moveLeft.restart(false);
			break;
	case Qt::Key_Right :
			if (m_grid.getShape() && !m_on_pause) m_grid.getShape()->moveRight.restart(false);
			break;
	#ifdef DEBUG
	case Qt::Key_Up :
			if (m_grid.getShape() && !m_on_pause) m_grid.getShape()->moveUp.restart(false);
			break;
	#endif
	case Qt::Key_Down :
			if (m_grid.getShape() && !m_on_pause) m_grid.getShape()->moveDown.restart(false);
			break;
	case Qt::Key_Space :
			m_on_pause = !m_on_pause;
			break;
	}
} /*}}}*/
